﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;

namespace TCV_WEB.Admin.Page
{



    public partial class News : System.Web.UI.Page
    {
        private int iNewcatid = 0;
        private String sErr = "";
        private String _sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {


            if (Session["IDCategoryNews"] != null)
            {
                iNewcatid = Convert.ToInt32(Session["IDCategoryNews"]);
                Session["IDCategoryNews"] = null;
            }

            NewsCategoriesControl newcatcontrol = new NewsCategoriesControl();

            NewsCategoriesInfo newcatinfo = newcatcontrol.Get(iNewcatid, ref sErr);
            lbnamenew.Text = newcatinfo.NameVN;

            if (!Page.IsPostBack)
            {
                LoadDataGrid();
            }

        }
        private void LoadDataGrid()
        {
            DataTable dtList = new DataTable();
            try
            {
                DataTable dtb;
                NewsControl new_control = new NewsControl();

                dtb = new_control.GetAll_News(iNewcatid, 0, ref _sErr);
                RadGrid1.DataSource = dtb;
                RadGrid1.DataBind();
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
            finally
            {
                dtList.Dispose();
            }
        }



        protected void btAddnew_Click(object sender, EventArgs e)
        {
            Session["IDCategoryNews"] = null;
            Session["newsid"] = null;
            Response.Redirect("~/Admin/Page/DetailNews.aspx");
        }

        protected void dtgnew_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void dtgnew_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            //dtgnew.CurrentPageIndex = e.NewPageIndex;
            //LoadDataGrid();
        }

        protected void dtgnew_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            //if (e.CommandName.ToUpper() == "DELETE")
            //{
            //    try
            //    {
            //        int inewsid = Convert.ToInt32(dtgnew.Items[e.Item.ItemIndex].Cells[0].Text.ToString());
            //        NewsControl new_control = new NewsControl();

            //        lblErr.Text = new_control.Delete_News(inewsid);
            //        LoadDataGrid();
            //    }
            //    catch (System.Exception ex)
            //    {
            //        lblErr.Text = ex.Message;
            //    }
            //}
            //if (e.CommandName.ToUpper() == "EDIT")
            //{
            //    int inewsid = Convert.ToInt32(dtgnew.Items[e.Item.ItemIndex].Cells[0].Text.ToString());
            //    Session["newsid"] = inewsid;
            //    Response.Redirect("~/Admin/Page/DetailNews.aspx");
            //}
        }

        protected void btAddnew0_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Page/NewsCategories.aspx");
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "DELETE")
            {
                try
                {
                    Int64 inewsid = Convert.ToInt64(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                    NewsControl new_control = new NewsControl();

                    lblErr.Text = new_control.Delete(inewsid);
                    RadGrid1.Rebind();
                }
                catch (System.Exception ex)
                {
                    //       lblErr.Text = ex.Message;
                }
            }
            if (e.CommandName.ToUpper() == "LOAD")
            {
                Int64 inewsid = Convert.ToInt64(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                Session["newsid"] = inewsid;
                Response.Redirect("~/Admin/Page/DetailNews.aspx");
            }
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            LoadDataGrid();
        }


    }
}
